package org.moonlight.jvm.instructions.stack.dup;

import org.moonlight.jvm.instructions.base.NoOperandsInstruction;
import org.moonlight.jvm.rtda.exclusive.Frame;
import org.moonlight.jvm.rtda.exclusive.OperandStack;
import org.moonlight.jvm.rtda.exclusive.Slot;

/**
 * DUP2: 复制 操作数栈栈顶 的一个或两个 变量
 *
 * bottom -> top
 * [...][c][b][a]____
 *           \____   |
 *                |  |
 *                V  V
 * [...][c][b][a][b][a]
 *
 * @author Moonlight
 * @createTime 2023/8/16 17:22
 **/
public class Dup2 extends NoOperandsInstruction {

    @Override
    public void execute(Frame frame) {
        OperandStack stack = frame.getOperandStack();

        Slot a = stack.popSlot();
        Slot b = stack.popSlot();

        stack.pushSlot(b);
        stack.pushSlot(a);
        stack.pushSlot(b);
        stack.pushSlot(a);
    }

}
